package com.lhoverc.controller.user;

import com.lhoverc.annotation.Log;
import com.lhoverc.pojo.Result;
import com.lhoverc.pojo.VO.ApplyDataVO;
import com.lhoverc.pojo.VO.TodayApplyVO;
import com.lhoverc.pojo.dto.AddApplyDto;
import com.lhoverc.service.apply.ApplyService;
import jakarta.mail.MessagingException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @Author: LiCheng
 * @CreateTime: 2025-04-16
 * @Description:
 * @Version: 1.0
 */
@Slf4j
@RestController
@RequestMapping("/user/apply")
public class ApplyController {
    @Autowired
    private ApplyService applyService;

    /**
     * 批同意申请
     *
     * @param ids
     * @return
     */
    @Log
    @PostMapping("/allowApply")
    public Result allowApply(@RequestBody List<Integer> ids) {
        applyService.allowApply(ids);
        return Result.success();
    }

    /**
     * 批拒绝申请
     *
     * @param ids
     * @return
     */
    @Log
    @PostMapping("/refuseApply")
    public Result refuseApply(@RequestBody List<Integer> ids) {
        applyService.refuseApply(ids);
        return Result.success();
    }

    /**
     * 条件查询申请数据
     *
     * @param userId
     * @param visitorName
     * @param state
     * @param startTime
     * @param endTime
     * @return
     */
    @GetMapping("/getApplyData")
    public Result getApplyData(Integer userId,
                               Integer state,
                               String visitorName,
                               @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime startTime,
                               @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime endTime) {
        List<ApplyDataVO> ApplyDataVOList = applyService.getApplyData(userId, visitorName, state, startTime, endTime);
        return Result.success(ApplyDataVOList);
    }

    /**
     * 获取当天申请列表
     * @param id
     * @return
     */
    @GetMapping("/getApplyById/{id}")
    public Result getApplyById(@PathVariable int id) {
        List<TodayApplyVO> todayApplyVOList = applyService.getApplyById(id);
        return Result.success(todayApplyVOList);
    }

    /**
     * 添加申请
     * @param addApplyDto
     * @return
     */
    @PostMapping("/addApply")
    public Result addApply(@RequestBody AddApplyDto addApplyDto) throws MessagingException {
        applyService.addApply(addApplyDto);
        return Result.success();
    }

    /**
     * 更改申请信息
     * @param applyDataVO
     * @return
     */
    @PostMapping("/updateApply")
    public Result updateApply(@RequestBody ApplyDataVO applyDataVO){
        applyService.updateApply(applyDataVO);
        return Result.success();
    }

    /**
     * 批量删除申请信息
     * @param ids
     * @return
     */
    @PostMapping("/deleteApply")
    public Result deleteApply(@RequestBody List<Integer> ids){
        applyService.deleteApply(ids);
        return Result.success();
    }
}
